package java51.lesson0619;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReaderXml {

	public static void main(String[] args) {
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read(new File("products.xml"));
			Element root = doc.getRootElement();
			String shopname = root.attributeValue("name");
			System.out.println("根目录信息是：" + shopname);
			List<Element> list = doc.selectNodes("/shop/product");
			for (Element e : list) {
				String sdis = e.attributeValue("discount");// 是否折扣
				String sfac = e.attributeValue("factoryaddr");// 工厂地址
				Element eitemno = (Element) e.selectSingleNode("itemno");
				String itemno = eitemno.getText();// 商品编号
				Element ename = (Element) e.selectSingleNode("name");
				String name = ename.getText();// 商品名称
				Element eprice = (Element) e.selectSingleNode("price");
				String price = eprice.getText();// 商品单价
				Element estockcount = (Element) e.selectSingleNode("stockcount");
				String stockcount = estockcount.getText();// 商品库存
				Element eunit = (Element) e.selectSingleNode("unit");
				String unit = eunit.getText();// 商品计价单位
				System.out.println("折扣:" + sdis + ",工厂地址:" + sfac + ",编号:" + itemno + ",名称:" + name + ",单价" + price
						+ ",库存:" + stockcount + ",计价单位：" + unit);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
